export default class StorageUtils {

    /** 存储数字（包括整数和小小数） */
    static saveNumber(key: string, value: number) {
        localStorage.setItem(key, value + '');
    }
    /** 读取数字（包括整数和小小数） */
    static readNumber(key: string, defaultValue = 0): number {
        let value = localStorage.getItem(key);
        if (value) {
            return Number(value);
        }
        return defaultValue;
    }


    /** 存储布尔值 */
    static saveBool(key: string, value: boolean) {
        localStorage.setItem(key, value + '');
    }
    /** 读取布尔值 */
    static readBool(key: string, defaultValue = false): boolean {
        let value = localStorage.getItem(key);
        if (value) {
            return Boolean(value);
        }
        return defaultValue;
    }


    /** 存储字符串 */
    static saveString(key: string, value: string) {
        localStorage.setItem(key, value);
    }
    /** 读取字符串 */
    static readString(key: string, defaultValue = null): string {
        let value = localStorage.getItem(key);
        if (value) {
            return value;
        }
        return defaultValue;
    }


    /** 存储对象（如：{name:'xiaoming',age:20}） */
    static saveData(key: string, value: {}) {
        localStorage.setItem(key, JSON.stringify(value));
    }
    /** 读取对象（如：{name:'xiaoming',age:20}） */
    static readData(key: string, defaultValue = null): {} {
        let value = localStorage.getItem(key);
        if (value) {
            return JSON.parse(value);
        }
        return defaultValue;
    }

}
